package com.alvis.media.controller.admin;

import com.alvis.media.base.BaseApiController;
import com.alvis.media.base.PythonVisualize;
import com.alvis.media.base.RestResponse;
import com.alvis.media.service.PythonAIAnalysisService;
import com.alvis.media.viewmodel.admin.analysis.AIAnalysisResultVM;
import com.alvis.media.viewmodel.admin.analysis.ModelEvaluationVM;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据分析控制器
 */
@RestController("AdminAnalysisController")
@RequestMapping(value = "/api/admin/analysis")
@AllArgsConstructor
public class AnalysisController extends BaseApiController {

    private final PythonAIAnalysisService pythonAIAnalysisService;

    /**
     * 执行模型训练并返回评估结果
     */
    @RequestMapping(value = "/model/evaluation", method = RequestMethod.POST)
    public RestResponse<List<ModelEvaluationVM>> runModelEvaluation() {
        try {
            // 调用Python脚本进行模型训练和评估
            List<ModelEvaluationVM> results = PythonVisualize.invokePythonVisualize();

            if (results == null || results.isEmpty()) {
                return RestResponse.fail(500, "模型评估失败，未获取到结果");
            }

            return RestResponse.ok(results);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(500, "执行模型评估时发生错误: " + e.getMessage());
        }
    }

    /**
     * 执行AI数据分析并返回完整结果
     */
    @RequestMapping(value = "/ai-analysis", method = RequestMethod.POST)
    public RestResponse<AIAnalysisResultVM> getAIAnalysis() {
        try {
            // 调用Python脚本进行AI数据分析
            AIAnalysisResultVM result = pythonAIAnalysisService.executeAIAnalysis();

            if (result == null) {
                return RestResponse.fail(500, "AI分析失败，未获取到结果");
            }

            return RestResponse.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(500, "执行AI分析时发生错误: " + e.getMessage());
        }
    }

}
